package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice2;
import cn.wolfcode.mapper.Notice2Mapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INotice2Service;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Notice2ServiceImpl implements INotice2Service {

    @Autowired
    private Notice2Mapper notice2Mapper;


    //插入数据的时候将响应的用户id和那个都插入进去
    public void save(Notice2 notice2, List<Employee> employees) {
        //创建人，创建时间，发布状态都需要默认设置
        Date date = new Date();
        //@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        notice2.setCreateTime(date);
        notice2.setEmployee(UserContext.getCurrentUser());
        notice2.setNoticeStatus(false);
        notice2Mapper.insert(notice2);

        notice2Mapper.insertRelationShip(notice2.getId(), employees);
    }

    @Override
    public void delete(Long id) {
        notice2Mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice2 notice2) {
        notice2Mapper.updateByPrimaryKey(notice2);
    }

    @Override
    public Notice2 get(Long id) {
        return notice2Mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice2> listAll() {
        return notice2Mapper.selectAll();
    }

    @Override
    public PageInfo<Notice2> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice2> notice2s = notice2Mapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Notice2>(notice2s);
    }



}
